﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using thousandclear.Data;
using thousandClear.Areas.Admin.Models;
using thousandClear.Util.Setting;

namespace thousandClear.Areas.Admin.Controllers
{
    public class AddressController : BaseController
    {
        // GET: Admin/Address
        public ActionResult Index(int? page, int? count, int? status,string no, string name)
        {
            string level = "1";
            level = GetCookie("level");
            ViewBag.level = level;
            page = page ?? 1;
            count = count ?? 10;
            int totalCount = 0;
            int totalPage = 1;
            int currentPage = page.Value;
            int pageCount = count.Value;

            List<AddressModel> list = new List<AddressModel>();

            using (var db = new qianjingEntities())
            {
                var query = db.O_Address.AsQueryable().Where(a => a.RowStatus == 1);

                if (!string.IsNullOrEmpty(no))
                {
                    query = query.Where(nos => nos.oasNo.Contains(no));
                }

                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(nos => nos.oasName.Contains(name));
                }

                totalCount = query.Count();
           
                var records = query.OrderByDescending(a => a.OASID).Skip((page.Value - 1) * count.Value).Take(count.Value).ToList();
                var addressNo = db.SP_GetRefNo().FirstOrDefault();
                foreach (var item in records)
                {
                   
                    var datDesc = db.D_Angent.FirstOrDefault(a => a.DATID == item.oasDATID);

                    if (datDesc != null)
                    {
                        var datDesc2 = db.D_Angent.FirstOrDefault(a => a.DATID == datDesc.datDATID);
                        var datDesc3 = db.D_Angent.FirstOrDefault(a => a.DATID == datDesc2.datDATID);

                        list.Add(new AddressModel
                        {
                            OASID = (int)item.OASID,
                            //order = item.toSerialNo,
                            oasName = item.oasName,
                            oasNo = item.oasNo,
                            osaPhone = item.osaPhone,
                            oasAddressDetail = datDesc3.datDesc + datDesc2.datDesc + datDesc.datDesc + item.oasAddressDetail,
                             CreatedBy = item.oasAddressDetail,
                            DATIDdesc = datDesc.datDesc,
                            oasPoint = (decimal)(item.oasPoint == null ? 0 : item.oasPoint),
                            stutas = item.oasOCID == null ? "未绑定" : "成功"
                        });
                    }
                    
                }
            }

            ViewBag.TotalCount = totalCount;
            totalPage = int.Parse((totalCount / count).ToString()) + (totalCount % count == 0 ? 0 : 1);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            ViewBag.PageCount = pageCount;

            using (var db = new qianjingEntities())
            {

                var Provinces = db.D_Angent.Where(a => a.RowStatus == 1 && a.datLevel == 1).ToList();
                ViewBag.Provinces = JsonConvert.SerializeObject(Provinces);

            }

            return View(list);
        }

        //data1 = '{"cName":"'+$("#cName").val()+'","cPhone":"'+$("#cPhone").val()+'","ddlBlock":"'+$("#ddlBlock").val()+'","cAddress":"'+$("#cAddress").val()+'","cPoint":"'+$("#cPoint").val()+'"}';
        [HttpPost]
        public string CreateAddress(string cName, string cPhone, string ddlBlock, string cAddress, string cPoint)
        {
            using (var db = new qianjingEntities())
            {
                string oasNo = "";
                oasNo = db.SP_GetRefNo().FirstOrDefault();
                int dblk = int.Parse(ddlBlock);
                decimal ss = decimal.Parse(cPoint);
                db.O_Address.Add(new O_Address
                {
                    oasDATID = dblk,
                    oasName = cName,
                    osaPhone = cPhone,
                    oasAddressDetail = cAddress,
                    RowStatus = 1,
                    CreatedDate = DateTime.Now,
                    oasNo = oasNo,
                    oasPoint = ss
                });


                db.SaveChanges();
            }
            return "1";
        }

        [HttpPost]
        public string updateList(string oasid, string phone,string point,string name,string casn)
        {
            string res = "1";
            using (var db = new qianjingEntities())
            {
                var yuo = db.O_Address.Where(oc => oc.oasNo.Trim() == casn.Trim()).Count();

                if (yuo>1)
                {
                    res = "-1";
                }
                else
                {
                    int so = int.Parse(oasid);
                    var ttc = db.O_Address.FirstOrDefault(oc => oc.OASID == so);
                    decimal ocg = decimal.Parse(string.IsNullOrEmpty(point) ? "0" : point);
                    ttc.osaPhone = phone;
                    ttc.oasPoint = ocg;
                    ttc.oasName = name;
                    ttc.oasNo = casn;
                    db.SaveChanges();

                    res = "1";
                }

                

            }
            return res;
        }

        [HttpPost]
        public void RemovAddress(string oasid)
        {
            using (var db = new qianjingEntities())
            {
                var query = db.O_Address.FirstOrDefault(od => od.OASID.ToString().Trim() == oasid.ToString().Trim());

                query.RowStatus = 0;

                db.SaveChanges();
            }
        }
    }
}